package cn.itcast.oa.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import cn.itcast.oa.base.BaseDAOimpl;
import cn.itcast.oa.cfg.Configuration;
import cn.itcast.oa.model.Forum;
import cn.itcast.oa.model.PageBean;
import cn.itcast.oa.model.Topic;
import cn.itcast.oa.service.TopicService;

@Service
@SuppressWarnings("unchecked")
public class TopicServiceImpl extends BaseDAOimpl<Topic> implements TopicService{

	@Override
	public List<Topic> findForum(Forum forum) {
		//AODO
		return getSession().createQuery(//
				"FROM Topic t WHERE t.forum=? ORDER BY (CASE t.type WHEN 2 THEN 2 ELSE 0 END) DESC, t.lastUpdateTime DESC")//
				.setParameter(0, forum)
				.list();
	}
	
	@Override
	public void save(Topic topic){
		topic.setLastUpdateTime(topic.getPostTime());
		getSession().save(topic);
		
		Forum forum=topic.getForum();
		forum.setLastTopic(topic);
		forum.setTopicCount(forum.getTopicCount()+1);
		forum.setArticleCount(forum.getArticleCount()+1);
		getSession().update(forum);
	}

//	@Override
//	public PageBean getPageBean(int pageNum, Forum forum) {
//		// TODO Auto-generated method stub
//		int pageSize=Configuration.getPageSize();
//
//	    List recordList=getSession().createQuery(//
//	    		"FROM Topic t WHERE t.forum=? ORDER BY (CASE t.type WHEN 2 THEN 2 ELSE 0 END) DESC, t.lastUpdateTime DESC")//
//	    		.setParameter(0, forum)
//	    		.setFirstResult((pageNum - 1) * pageSize)
//	    		.setMaxResults(pageSize)
//	    		.list();
//		Long recordCount=(Long) getSession().createQuery(//
//				"SELECT COUNT(*) FROM Topic t WHERE t.forum=?")//
//				.setParameter(0, forum)
//				.uniqueResult();
//		return new PageBean(pageNum,pageSize,recordList,recordCount.intValue());
//	}
	
}
